<?php

/**
 *      [品牌空间] (C)2001-2010 Comsenz Inc.
 *      This is NOT a freeware, use is subject to license terms
 *
 *      $Id: attend.php 5720 2011-06-29 07:11:58Z menglingmin $
 */

require_once 'common.php';
if (!$_G['setting']['multipleshop']) {
	if(intval($_G['myshopid']) > 0){
		showmessage($lang['oneowner_onshop'], "index.php");
	}
}

$do = $_GET['do'];
require_once(B_ROOT.'./uc_client/client.php');
include_once(B_ROOT.'./source/function/cache.func.php');
require_once B_ROOT . 'source/class/attend.class.php';

//读入缓存
$mname = 'shop';
$cacheinfo = getmodelinfoall('modelname', $mname);
$mid = $cacheinfo['models']['mid'];

if($do && $do=="register") {
	//第三步，提交数据，检查填写的基本信息
	if(submitcheck('attendsubmit')) {

		$checkunits = attend::getCheckUnits();

		foreach($checkunits as $unit) {
			$unit_len = bstrlen($_POST[$unit[0]]);
			if($unit_len == 0 /* 可能配置得时候不允许为空但长度却为0 */
				|| $unit_len < $unit[1] || $unit_len > $unit[2]
				|| (in_array($unit[0], array('applicantmobi', 'applicantpost')) && !is_numeric($_POST[$unit[0]]))) {
				$_G['ajax_errors'][] = array($unit[0]=>$unit[3]);
			}
		}
		if($_POST['catid'] < 1) {
			$_G['ajax_errors'][] = array('catid'=>$lang['attend_cat_must_select']);
		}
		if($_POST['region'] == -1) {
			$_G['ajax_errors'][] = array('region'=>$lang['attend_region']);
		}
		display_errors('applicant_info_failed');

		if (!$_G['uid']) {
			$username = addslashes(trim(stripslashes($_POST['username'])));
			$password = addslashes(trim(stripslashes($_POST['password'])));
			$email = $_POST['email'];
			$existAcc = intval($_POST['existuser']) === 1 ? true:false;
			if (strlen($username) < 3 || strlen($username) > 15) {
				$_G['ajax_errors'][] = array('username'=>$lang['attend_username_error']);
			}
			if (!$password) {
				$_G['ajax_errors'][] = array('password'=>$lang['password_can_not_null']);
			}

			display_errors('applicant_info_failed');

			if ($existAcc) {
				$member = uc_user_login($username, $password);
				if ($member[0] < 1) {
					$_G['ajax_errors'][] = array('password'=>$lang['username_or_password_error']);
					display_errors('applicant_info_failed');
				}
				$member['uid'] = $member[0];
				$member['username'] = $member[1];
			} else {
				if ($password && $_POST['password_1'] !== $password) {
					$_G['ajax_errors'][] = array('password_1'=>$lang['attend_password_repeat']);
				}
				if (!$email) {
					$_G['ajax_errors'][] = array('email'=>$lang['attend_email_is_null']);
				}
				display_errors('applicant_info_failed');

				$member = attend::registerUser($username, $password, $email);
			}
			if ($member['uid'] > 0) {
				$_G['uid'] = $member['uid'];
				$_G['username'] = $member['username'];

				if (!$_G['setting']['multipleshop']) {
					// grade = 3 已审核， grade ＝ 0 未审核
					$myshopid = DB::result_first("select count(itemid) from " . DB::table("shopitems") . " where (grade=3 or grade = 0) and uid=".$_G['uid']);
					if ($myshopid > 0) {
						$_G['ajax_errors'][] = array('username'=>$lang['oneowner_onshop']);
						display_errors('applicant_info_failed');
					}
				}

				$cookievalue = authcode(md5($password)."\t" . $_G['uid'], 'ENCODE');
				ssetcookie('auth', $cookievalue, 0, 1, true);
			}
		}

		$shop['uid'] = $_G['uid'];
		$shop['username'] = $_G['username'];
		$shop['dateline'] = $_G['timestamp'];
		$shop['subject'] = addslashes(trim(stripslashes($_POST['subject'])));
		$shop['address'] = addslashes(trim(stripslashes($_POST['address'])));
		$shop['region'] = $_POST['region'];

		foreach($cacheinfo['columns'] as $column) {
			if (($column['allowpost'] == 1) && ($column['allowshow'] == 1) && ($column['formtype'] != 'img')){
				if(preg_match('/(^ext_)|(^applicant)/',$column['fieldname'])){
					if(!empty($_POST[$column['fieldname']])){
						$itemmessage[$column['fieldname']] = $_POST[$column['fieldname']];
					}
				} elseif(in_array($column['fieldname'], array('subject','subjectimage','address','tel','catid'))){
					if(!empty($_POST[$column['fieldname']])){
						$shop[$column['fieldname']] = $_POST[$column['fieldname']];
					}
				}
			}
		}
		$shop['letter'] = getletter($shop['subject']);
		$shop['address'] = $_POST['address'];
		$shop['catid'] = $_POST['catid'];
		$shop['validity_end'] = mktime(0,0,0,date('m',$_G['timestamp']),date('d',$_G['timestamp']), (date('Y',$_G['timestamp']) + 10));
		// 自动为用户加商店
		$shopid = inserttable('shopitems',$shop,1);

		$itemmessage['itemid'] = $shopid;
		$itemmessage['postip'] = $_G['clientip'];
		inserttable('shopmessage',$itemmessage);

		!pkperm('isadmin') && DB::query('UPDATE '.DB::table('members')." SET myshopid='$shopid' WHERE uid='$_G[uid]'");

		$ucsynlogin = uc_user_synlogin($_G['uid']);
		if ($_G['setting']['auditnewshops']) {
			attend::sendMsg();
			showmessage('apm_panel_msg', 'index.php', 0, array('ucsynlogin' => $ucsynlogin));
		} else {
			// 更新缓存
			$_BCACHE->deltype('sitelist', 'attr');
			$_BCACHE->deltype('sitelist', 'shop');
			$_BCACHE->deltype('storelist', 'shop', $shopid);
			$_BCACHE->deltype('detail', 'shop', $_POST['itemid']);
			DB::query('UPDATE '.DB::table('shopitems')." SET grade='3', groupid='{$_G['setting']['defaultshopgroup']}' WHERE itemid='$shopid'");
			showmessage('attend_register_success', 'index.php', 0, array('ucsynlogin' => $ucsynlogin));
		}

		exit;

	} else {
		showmessage('no_submit', 'attend.php');
	}
} elseif ($do && $do=="attend") {

	$showarr = getcategory('region');
	$arr_cat = getcategory('shop');
	$value = '';
	$regionlist = $showarr;
	include template('templates/site/default/attend_step2.html.php', 1);
} else {
	//第一步
	include template('templates/site/default/attend_step1.html.php', 1);
}

ob_out(); //正则处理url/模板

?>
